Skip to content

Trim cgp::prelude to the names users write - #294

Merged
soareschen merged 4 commits into
mainfrom
prelude-trim
Nov 14, 2026
Merged

soareschen merged 4 commits into
mainfrom
prelude-trim

Conversation

@soareschen

@soareschen soareschen commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Overview

[claude-opus-5-5] use cgp::prelude::* now brings in only the names users write: the macros and derives, the error traits HasErrorType, CanRaiseError, and CanWrapError, the UseType and UseDelegate providers, DefaultNamespace, and PhantomData. Before this PR, cgp::macro_prelude was an alias of the prelude, so every name the generated code refers to, such as IsProviderFor, DelegateComponent, the field traits, HasType, and the handler traits, came into user modules with the glob.

cgp::macro_prelude is now a module of its own in cgp, cgp-core, and cgp-extra, holding the full previous set, so every path the macros generate still resolves. Its doc comment says that only the macros use it and sends readers to cgp::prelude or the module that defines a name, since rustc's import hints can suggest it.

Impact

This is a breaking change for code that used a dropped name through the glob. Each dropped name stays importable from its module:

  • Wiring machinery: IsProviderFor, DelegateComponent, CanUseComponent, RedirectLookup, UseContext, UseFields, WithContext, and WithProvider, from cgp::core::component.
  • Fields and paths: the field-access, builder, extractor, and shape traits, such as HasField, HasFields, HasBuilder, and BuildField, from cgp::core::field::traits; UseField and the field-mode markers such as IsPresent and IsRef, from cgp::core::field::impls; the type-level types Cons, Nil, Either, Void, Field, Index, and Life, with Chars, MRef, and Symbol, from cgp::core::field::types; and PathCons and ConcatPath, from cgp::core::base::types and cgp::core::base::traits.
  • Abstract types: HasType and TypeProvider, from cgp::core::types.
  • Handlers: the provider traits such as Computer and Handler, their component keys, and the promotion bundles such as PromoteComputer, from cgp::extra::handler. The prelude keeps only the #[cgp_computer], #[cgp_producer], and #[cgp_auto_dispatch] macros.
  • Dispatch: the value-handler matchers, from cgp::extra::dispatch.

Derives and type-level macros stay in the prelude where their traits and types leave. #[derive(HasField)], HasFields, BuildField, ExtractField, and FromVariant work without an import, while a bound on one of those traits or a call to its methods needs one, and Product!, Sum!, and Symbol! stay while the types they expand to leave.

A provider written with #[cgp_impl] on Computer imports ComputerComponent as well as Computer. The macro names the key as {Trait}Component in the caller's scope.

Plain cargo check output qualifies a dropped name through cgp::macro_prelude. When a module does not import the name, rustc writes cgp::macro_prelude::HasField in place of the earlier cgp::prelude::HasField, which is why the dependents' compile-fail snapshots change.

The dependent repositories pass CI only once this PR merges. cgp-examples, cgp-serde, Hypershell, the website's example code, and cargo-cgp's fixtures import what they lose and expect the trimmed prelude in their snapshots, as the crates and tests in this repository do. Hypershell's own prelude exports the handler traits and keys, since its programs are handler pipelines, and its modules import Cons and Nil where they write them.

The doc that #[cgp_impl(new …)] and #[cgp_new_provider] give a provider struct now links its trait by the full path. It linked the last path segment, which resolved only while the prelude put the handler traits in scope. A #[cgp_computer] provider's doc now links cgp::macro_prelude::Computer, which rustdoc resolves to the trait's own page, and cargo doc runs without broken-link warnings.

Areas needing attention

The keep list in crates/main/cgp-core/src/prelude.rs and crates/main/cgp-extra/src/prelude.rs needs the most judgment. A name it leaves out that users commonly write would break code the docs show. The doc-link change is in crates/macros/cgp-macro-core/src/types/cgp_provider/item.rs.

soareschen and others added 4 commits November 14, 2026 15:44
`cgp::macro_prelude` was an alias of the prelude, so every name the
macros' generated code refers to had to be glob-imported into user
modules. It is now a module of its own in `cgp`, `cgp-core`, and
`cgp-extra`, holding the full previous set, and the prelude keeps only
the macros, derives, traits, providers, type-level types, and handler
traits and keys that users write.

The dropped names stay importable from their modules: the wiring
machinery (`IsProviderFor`, `DelegateComponent`, `CanUseComponent`,
`RedirectLookup`) from `cgp::core::component`, the path, field, and
extractor traits and markers from `cgp::core::field` and
`cgp::core::base`, `HasType` and `TypeProvider` from `cgp::core::types`,
and the reference handler forms, `Promote*` adapters, and value-handler
matchers from `cgp::extra::handler` and `cgp::extra::dispatch`. A
derive keeps its name in the prelude where its trait leaves, as
`ExtractField` and `FromVariant` do, and `Symbol!` stays while the
`Symbol` type leaves.

This is a breaking change for code that used a dropped name through the
glob. The crates and tests here import those names from their modules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The handler macros reach every name they emit through
`cgp::macro_prelude`, so the prelude no longer exports `cgp_handler`'s
traits and keys: `Computer`, `AsyncComputer`, `TryComputer`, `Handler`,
`Producer`, and their component keys. Code that writes a provider or a
wiring entry for them imports them from `cgp::extra::handler`, as code
that calls a handler already did for the consumer traits. `UseContext`
leaves the prelude too and stays in `cgp::core::component`.

The provider struct's generated doc linked its trait by the last path
segment, which resolved only while the prelude put the handler traits
in scope. A multi-segment trait path now links its full path, so
`#[cgp_computer]` output links `cgp::macro_prelude::Computer` wherever
it expands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`cgp::prelude` no longer re-exports `UseField`, `BuildField`,
`FinalizeBuild`, `HasBuilder`, `HasField`, `HasFields`, `Cons`,
`Either`, `Field`, `Index`, `Life`, `Nil`, or `Void`. The macros reach
them through `cgp::macro_prelude`, so most code never names them; code
that does imports them from `cgp::core::field::impls`, `traits`, or
`types`. The derives of the same names stay in the prelude.

The tests import the names they use, and the test modules left with no
use of the glob drop it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each `macro_prelude` holds the names the macros' generated code refers
to. Its doc comment now says that only the macros use it and points
readers to `cgp::prelude` or the module that defines a name, since
rustc's import hints and docs.rs both show the module to users.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@soareschen
soareschen marked this pull request as ready for review November 14, 2026 18:43
@soareschen
soareschen merged commit 1d1eff9 into main Nov 14, 2026
6 checks passed
@soareschen
soareschen deleted the prelude-trim branch November 14, 2026 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant